﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Celsius.AnimeTools.AssAnalytic
{
	public class AnimeAssAnalytic
	{
		/// <summary>
		/// 构造函数。
		/// </summary>
		/// <param name="assFileName">输入ass文件完整路径。</param>
		public AnimeAssAnalytic(string assFileName)
		{
			this.ScriptInfo = new ScriptInfo();
			this.V4Styles = new V4Styles();
			this.Events = new Events();
			this.AnalyzeASS(assFileName);
		}

		/// <summary>
		/// 头信息。
		/// </summary>
		public ScriptInfo ScriptInfo
		{
			get;
			set;
		}

		/// <summary>
		/// 字体信息。
		/// </summary>
		public V4Styles V4Styles
		{
			get;
			set;
		}

		/// <summary>
		/// 内容。
		/// </summary>
		public Events Events
		{
			get;
			set;
		}

		/// <summary>
		/// 分析ass文件，填充各个变量。
		/// </summary>
		/// <param name="fileFullName">要分析的ass文件绝对路径。</param>
		private void AnalyzeASS(string fileFullName)
		{
			using (StreamReader sr = new StreamReader(fileFullName))
			{
				var part = ASSParts.ScriptInfo;
				while (!sr.EndOfStream)
				{
					string s = sr.ReadLine();
					if (Regex.IsMatch(s, @"\s*\[Script\s+Info\]\s*"))
						part = ASSParts.ScriptInfo;
					else if (Regex.IsMatch(s, @"\s*\[V4\+\s+Styles\]\s*"))
						part = ASSParts.Styles;
					else if (Regex.IsMatch(s, @"\s*\[Events\]\s*"))
						part = ASSParts.Events;
					else
						switch (part)
						{
							case ASSParts.ScriptInfo:
								{
									this.ScriptInfo.Build(s);
									break;
								}
							case ASSParts.Styles:
								{
									this.V4Styles.Build(s);
									break;
								}
						}
				}
			}
		}

	}
}
